﻿using System;
using RayDen.Library.Core;

namespace RayDen.Library.Components.Volume
{
    public class DensityField : AbstractField<float>
    {
        public override float this[int x, int y, int z]
        {
            get
            {
                return this.GetValue(x, y, z);
            }
        }

        private float GetValue(int x, int y, int z)
        {

            var p = this[x, y, z];

            var minX = x-1;
            var maxX = x+1;
                       
            var minY = y-1;
            var maxY = y+1;
                       
            var minZ = z-1;
            var maxZ = z+1;

            var p0 = this[minX, minY, minZ];
            var p1 = this[maxX, minY, minZ];
            var p2 = this[minX, maxY, minZ];
            var p3 = this[minX, minY, maxZ];
            var p4 = this[maxX, maxY, minZ];
            var p5 = this[minX, maxY, maxZ];
            var p6 = this[maxX, minY, maxZ];
            var p7 = this[maxX, maxY, maxZ];

            return (MathLab.Lerp(p, p0, p1) + MathLab.Lerp(p, p2, p3) + MathLab.Lerp(p, p0, p1)) / 3;

        }

        public DensityField(int w, int h, int d)
            : base(w, h, d)
        {
        }
    }
}